﻿using System.Collections;
using UnityEngine;

public class DrawLineWithPoint : MonoBehaviour {

	private GameObject liner;
	private LineRenderer line;
	private int numy;
	private float startx, starty;
	private float[] pointy;
	private int timer;
// 改动；
	public int potNum = 100;
	public float scaleY = 1, lineLen = 14;
	public float startWidth = 0.1f, endWidth = 0.08f;
	void Start () {
		// 初始化
		timer = 0;
		numy = potNum * 2;	// 两倍长度用作移除bug
		startx = this.GetComponent<Transform>().position.x;
		starty = this.GetComponent<Transform>().position.x;
		pointy = new float[numy];
		//实例化对象
		liner = new GameObject("liner");
		line = liner.AddComponent<LineRenderer>();
		//设置起始和结束的颜色
		// line.SetColors (Color.green, Color.green);
		// 设置材质
		line.material = this.GetComponent<MeshRenderer>().material;
		//设置起始和结束的宽度
		line.startWidth = startWidth;
		line.endWidth = endWidth;
		// 绘制线段
		line.positionCount = potNum;
	}

	// Update is called once per frame
	void Update () {

		//定帧执行
		if (Time.frameCount % 3 == 0) {
			int i;
			for (i = 0; i < potNum; i++) {
				//从左向右
				float point_x = startx - lineLen + i * lineLen / potNum;
				float point_y = starty + scaleY * pointy[(i + timer) % numy];
				line.SetPosition (i, new Vector3 (point_x, point_y, 15));
			}
			// 读取最新的y值
			float inputy = Mathf.Sin(0.3f*timer)*2;
			pointy[(i + timer) % numy] = inputy;
			// 右移
			timer++;
			// 设置点的位置
			transform.position = new Vector3(startx, starty+inputy, 16);
		}
	}
}